/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#ifndef BACKENDPLAINTEXT_H
#define BACKENDPLAINTEXT_H

#include <QFile>
#include <QObject>
#include <QTextStream>

#include <backendinterface.h>

/**
 * Plain text backend for saving bills.
 * Note: ;^; is used as a separator for each field in a bill. Bills are
 * separated by a new line. :n: and :r: are used in the file to replace
 * \n and \r. This is a simple backend for saving to a text file. The file
 * should not be edited by hand as there is no error checking for invalid data.
 * The backend assumes the order of the data.
 */
class BackendPlainText : public QObject, public BackendInterface
{
    Q_OBJECT
    Q_INTERFACES(BackendInterface)

    public:
        BackendPlainText();
        ~BackendPlainText();

        /**
        * openCollection does not use username, loginname or password. It opens
        * a plain text file on disk and is intended for single users.
        */
        bool openCollection(BackendOpenMode::BackendOpenModes openMode,
            const QString &location,
            const QString &username="", const QString &loginname="",
            const QString &password="");
        bool isCollectionOpen();
        void closeCollection();

        /**
         * This backend is able to read incrementally. All read functions are
         * implemented.
         */
        bool canReadIncremental();
        bool readAllBillItems(QSet<BillItem> &billItems);
        bool startReadNextBillItem();
        bool hasMoreBillItems();
        bool readNextBillItem(BillItem &billItem);

        bool saveAllBillItems(QSet<BillItem> billItems);
        /**
         * This backend cannot save Transactionally.
         */
        bool canSaveTransactional();
        /**
         * This backend can save incrementally.
         */
        bool canSaveAllIncremental();
        bool saveChange(Transaction transaction);
        bool saveBillItem(const BillItem &billItem);

        QString getLastErrorMessage();

        bool isRemote();
        QString getName();
        QString getDescription();
        QString getVersion();

        QString getDefaultFileExtenstion();

    private:
        QString m_errorMessage;

        QFile *m_plainTextFile;
        QTextStream *m_plainTextStream;
};

#endif /* BACKENDPLAINTEXT_H */
